﻿package com.webbei.custom.dao.impl;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.ServletActionContext;

import com.webbei.custom.dao.CustomDao;
import com.webbei.myCustom.action.MyUtil;
import com.webbei.pojo.BgPic;
import com.webbei.pojo.PicInfor;

public class CustomDaoImpl implements CustomDao {

	private SqlSession session;
	public List getBgPic(int id) {
		return session.selectList("BgPic.getBgPic", id);
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public String drawWord(String word, int size, String color, String family) {
		String filename = "";
		String path=ServletActionContext.getServletContext().getRealPath("/");
		size=200;
		try {
			Map<String,Integer> map = MyUtil.getStrigWidthAndHeight(word, new Font(family,Font.PLAIN,size));
			int width = map.get("width");
			int height = map.get("height");

			BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);

			Graphics2D ig2 = bi.createGraphics();
			bi = ig2.getDeviceConfiguration().createCompatibleImage(width,height, Transparency.TRANSLUCENT);

			ig2.dispose();

			ig2 = bi.createGraphics();

			Font font = new Font(family, Font.PLAIN, 200);
			ig2.setFont(font);
			String message = word;

			FontMetrics fontMetrics = ig2.getFontMetrics();
			int stringWidth = fontMetrics.stringWidth(message);
			int stringHeight = fontMetrics.getAscent();

			String temp = color.substring(4, color.length() - 1);
			String[] temp2 = temp.split(", ");
			ig2.setPaint(new Color(Integer.parseInt(temp2[0]), Integer.parseInt(temp2[1]), Integer.parseInt(temp2[2])));
			//ig2.drawString(message, (width - stringWidth) / 2, height / 2+ stringHeight / 2);
			ig2.drawString(message, (width - stringWidth) / 2, stringHeight);
			filename = "temp/" + String.valueOf((new Date().getTime()))+String.valueOf(new Random().nextInt(3)) + ".png";
			System.out.println(path+filename);
			ImageIO.write(bi,"PNG",new File(path+filename));
			System.out.println("文字图片生成");
		} catch (IOException ie) {
			ie.printStackTrace();
		}
		return filename;
	}
	public String drawPic(String bgPic, List<PicInfor> pi) {
		System.out.println("进入合成图片的实现");
		String path=ServletActionContext.getServletContext().getRealPath("/");
		System.out.println("path:"+path);
		String filename = "";
		try {
			BufferedImage bi = new BufferedImage(630, 480,BufferedImage.TYPE_INT_ARGB);
			Graphics2D ig2 = bi.createGraphics();
			bi = ig2.getDeviceConfiguration().createCompatibleImage(630, 480,Transparency.TRANSLUCENT);
			ig2.dispose();
			ig2 = bi.createGraphics();
			System.out.println("背景图片："+bgPic);
			BufferedImage bibg = ImageIO.read(new File(path+bgPic));
			ig2.drawImage(bibg, 0, 0, 630, 480, null);
			BufferedImage pic;
			for(PicInfor pif : pi){
				System.out.println("自定义图片："+pif.getPicUrl());
				pic = ImageIO.read(new File(path+pif.getPicUrl()));
				ig2.drawImage(pic, pif.getLeft(), pif.getTop(), pif.getWidth(), pif.getHeight(), null);
			}
			filename = "temp/" + String.valueOf((new Date().getTime()))
					+ String.valueOf(new Random().nextInt(3)) + ".png";
			ImageIO.write(bi, "PNG", new File(path+filename));
			/*path+filename：完整图片路径*/
			System.out.println("混合图片合成，合成图片地址为："+filename);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filename;
	}

	@Override
	public List<BgPic> getBgPicByProId(int productId) {
		// TODO Auto-generated method stub
		return session.selectList("BgPic.getBgPic2",productId);
	}

}
